package com.example.hyznbk.presenter;

import com.example.hyznbk.model.api.BaseReviewWrongApi;
import com.example.hyznbk.model.result.BaseReviewWrongEntity;
import com.example.hyznbk.presenter.presenter_interface.IBaseReviewWrongPresenter;
import com.example.hyznbk.view.base.BaseFragment;
import com.example.hyznbk.view.base.ILoadMoreDataView;
import com.lancoo.rxretrofitlibrary.Api.UserInfo;
import com.lancoo.rxretrofitlibrary.http.HttpManager;
import com.lancoo.rxretrofitlibrary.listener.HttpOnNextListener;
import com.lancoo.rxretrofitlibrary.subscribers.ProgressSubscriber;

import java.util.List;

import lancooUpLoad.PaperInfor;
import testPaperWrong.WrongQuesPaperActivity;
import testPaperWrongHistory.HistoryWrongQuesPaperActivity;

/**
 * Created by tangminglong on 17/8/11.
 */

public class BaseReviewWrongPresenter implements IBaseReviewWrongPresenter {
    private ILoadMoreDataView mView;
    private int pageIndex = 1;
    private int quesStatus = 0;//默认是未完成
    private String quesType = "";
    private int lastPosition = 0;

    private int wrongFrom;
    private ProgressSubscriber mSubscriber;
    private HttpOnNextListener<BaseReviewWrongEntity> httpOnNextListener = new HttpOnNextListener<BaseReviewWrongEntity>() {
        @Override
        public void onNext(BaseReviewWrongEntity baseReviewWrongEntity) {
            if (baseReviewWrongEntity.getTotalCount() > 0) {
                mView.hideLoading();
                List<BaseReviewWrongEntity.ModelListBean> modelList = baseReviewWrongEntity.getModelList();
                for (int i = 0 ; i < modelList.size() ; i ++){ //预处理题库返回不合理的地方
                    String summary = modelList.get(i).getQuesBrief();
                    /*summary = TagHelperUtils.replacAllTag(summary,"<P>","");
                    summary = TagHelperUtils.replacAllTag(summary,"<BR>","");
                    summary = TagHelperUtils.replaceNULL_U_Tag(summary);
                    summary = TagHelperUtils.replaceImageTag(summary);
                    if (summary.length() >100 &&
                            !(summary.contains("<table") || summary.contains("<TABLE"))){
                        summary = summary.substring(0,100);
                    }*/

                    String regexStr = "<[^>]*>"; //去除所有的标签
                    summary = summary.replaceAll(regexStr, "");
                    if (summary.length() > 100){
                        summary = summary.substring(0,100);
                    }
                    modelList.get(i).setQuesBrief(summary);
                }
                if (pageIndex == 1) {
                    mView.loadData(baseReviewWrongEntity);
                    lastPosition = 0;
                } else {
                    mView.loadMoreData(baseReviewWrongEntity);
                }

            }else {
                mView.showNoData(wrongFrom == 0 ?"暂无基本复习错题" : "暂无难题复习错题");
            }
        }


        @Override
        public void onError(Throwable e) {
            super.onError(e);
            mView.showNetError();
        }
    };

    public BaseReviewWrongPresenter(ILoadMoreDataView mView,int wrongFrom) {
        this.mView = mView;
        this.wrongFrom = wrongFrom;
    }

    private void getData(){
        if (mSubscriber != null && !mSubscriber.isUnsubscribed()){
            mSubscriber.unsubscribe();
        }
        BaseReviewWrongApi api = new BaseReviewWrongApi(httpOnNextListener,mView.getHolderActivity());
        api.setPageIndex(pageIndex);
        api.setQuesType(quesType);
        api.setQuesStatus(quesStatus);
        api.setQuesFrom(wrongFrom);
        api.setPageSize(lastPosition + 10);
        mSubscriber = new ProgressSubscriber(api);
        HttpManager.getInstance().doHttpDeal(api,mSubscriber);
    }
    @Override
    public void getData(boolean isShowing) {
        if (isShowing) mView.showLoading();
        pageIndex = 1;
        getData();
    }

    @Override
    public void getMoreData() {
        pageIndex = pageIndex + 1;
        getData();
    }

    @Override
    public void getDataByStatus(int quesStatusId) {
        if (quesStatus == quesStatusId) return;
        quesStatus = quesStatusId;
        getData(false);
    }

    @Override
    public void getDataByQuesType(String quesTypeId) {
        if (quesType.equals(quesTypeId)) return;
        quesType = quesTypeId;
        getData(false);

    }

    @Override
    public void openItemDetail(int position, BaseReviewWrongEntity.ModelListBean modelListBean) {
        lastPosition = position;
        //ToastUtils.showShort("点击第"+(position+1)+"个");
        UserInfo.PaperStatus = modelListBean.getState();
        UserInfo.PaperFrom = modelListBean.getQuesFrom();
        //TODO 调用下面方法进行跳转
        PaperInfor.ModelFrom = 1;//错题
        @PaperDef.PaperFrom int from;
        if (modelListBean.getQuesFrom() == 0)
            from = PaperDef.FROM_BASE_REVIEW_WRONG;
            else
            from = PaperDef.FROM_DIFFERENT_REVIEW_WRONG;

        WrongQuesPaperActivity.launch((BaseFragment) mView,modelListBean,from);
    }

    @Override
    public void openItemHistoryDetail(int position, BaseReviewWrongEntity.ModelListBean modelListBean) {
        lastPosition = position;
//        ToastUtils.showShort("历史推荐题  position: "+position);
        UserInfo.PaperStatus = modelListBean.getState();
        UserInfo.PaperFrom = modelListBean.getQuesFrom();
        @PaperDef.PaperFrom int from;
        if (modelListBean.getQuesFrom() == 0)
            from = PaperDef.FROM_BASE_REVIEW_WRONG_History;
        else
            from = PaperDef.FROM_DIFFERENT_REVIEW_WRONG_History;

        //可以自己预存一些变量 然后进行跳转
        HistoryWrongQuesPaperActivity.launch((BaseFragment) mView,modelListBean,from);
    }
}
